package cn.itsource.system.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.util.PageList;
import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.MenuMapper;
import cn.itsource.system.mapper.PermissionMapper;
import cn.itsource.system.service.IRoleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import com.mysql.jdbc.UpdatableResultSet;
import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public void add(Role role) {
        // 添加角色表
        super.add(role);
        //添加菜单权限
        permissionMapper.addMiddle(role.getId(),role.getPermissions());

        // 添加 按钮劝下你
        menuMapper.addMiddle(role.getId(),role.getMenus());
    }

    @Override
    @Transactional
    public void update(Role role) {
        // 修改角色
        super.update(role);
        // 删除 菜单 中间表
        permissionMapper.deleteMiddle(role.getId());

        // 删除 按钮中间表
        menuMapper.deleteMiddle(role.getId());
        //添加菜单权限
        permissionMapper.addMiddle(role.getId(),role.getPermissions());

        // 添加 按钮劝下你
        menuMapper.addMiddle(role.getId(),role.getMenus());

    }
}
